﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TTSBLL;
using TTSModel;
using ZedGraph;
using ZedGraph.Web;

public partial class QualityControl_T0002038_CPKbyDT : System.Web.UI.Page
{    
    //private string message;
    private AxisValue axisValueToGraph;
 
    private string parameterName { get { return ViewState["parameterName"].ToString(); } set { ViewState["parameterName"] = value; } }

    private IList<AxisValueInfo> AxisValueList;
    private CPKParameterValueInfo _CPKParameterValueInfo;
    private CPKParameterValue _CPKParameterValue;
    private string startSN { get { return ViewState["startSN"].ToString(); } set { ViewState["startSN"] = value; } }
    private string endSN { get { return ViewState["endSN"].ToString(); } set { ViewState["endSN"] = value; } }
    private string testType { get { return ViewState["testType"].ToString(); } set { ViewState["testType"] = value; } }
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
            parameterName = Request.QueryString["MeasurementName"];
            this.Label13.Text = parameterName;
            string startSNTek = Session["startSN"].ToString();
            string endSNTek = Session["endSN"].ToString();
            testType =Session["testType"].ToString();
            startSN = startSNTek.Substring(startSNTek.Length - 9, 9);
            endSN = endSNTek.Substring(endSNTek.Length - 9, 9);
            _CPKParameterValueInfo = _CPKParameterValue.CPKParameterValue_GetbySNRange("T0002038_CPKParaValueSNRange", startSN, endSN, parameterName, testType);
            AxisValueList = axisValueToGraph.AxisValue_GetbySNRange("T0002038_CPKBySNRange", startSN, endSN, parameterName, testType);
            this.ZedGraphWeb1.Visible = true;
        
            ShowParameterValue();
        }
    }
    public QualityControl_T0002038_CPKbyDT()
    {
        axisValueToGraph = new AxisValue();
        _CPKParameterValueInfo = new CPKParameterValueInfo();
        _CPKParameterValue = new CPKParameterValue();
    }
  
    protected void Check_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/QualityControl/T0002028 _SummaryReport.aspx");
      
    }
   
    private TextObj ReturnTextObj(TextObj _ReturnTextObj, string itemString, double X, double Y)
    {
        _ReturnTextObj = new TextObj(itemString, X, Y);
        _ReturnTextObj.Location.CoordinateFrame = CoordType.AxisXYScale;
        _ReturnTextObj.FontSpec.Size = 10;
        _ReturnTextObj.Location.AlignH = AlignH.Center;
        _ReturnTextObj.Location.AlignV = AlignV.Bottom;
        _ReturnTextObj.FontSpec.Border.IsVisible = false;
        _ReturnTextObj.FontSpec.Angle = 0;
        _ReturnTextObj.FontSpec.Fill.IsVisible = false;
        return _ReturnTextObj;
    }
    private void ShowParameterValue()
    {
        this.LbMean.Text = "Mean =" + _CPKParameterValueInfo.Mean;
        this.Lbσ.Text = "σ=" + _CPKParameterValueInfo.StandardDeviation;
        this.LbUSL.Text = "USL =" + _CPKParameterValueInfo.USL;
        this.lbZ3σ.Text = "+3σ=" + _CPKParameterValueInfo.PositiveThreeSD;
        this.lbF3σ.Text = "-3σ=" + _CPKParameterValueInfo.NegativeThreeSD;
        this.lbLSL.Text = "LSL =" + _CPKParameterValueInfo.LSL;
        this.lbTotalCount.Text = "Total count =" + _CPKParameterValueInfo.TotalCount;
        this.lbFaiCount.Text = "Fail count =" + _CPKParameterValueInfo.FailCount;
        this.lbCp.Text = "CP =" + _CPKParameterValueInfo.CP;
        this.lbCPU.Text = "CPU =" + _CPKParameterValueInfo.CPU;
        this.lbCPL.Text = "CPL =" + _CPKParameterValueInfo.CPL;
        this.lbCPK.Text = "CPK =" + _CPKParameterValueInfo.CPK;
    }
    protected void ZedGraphWeb1_RenderGraph(ZedGraphWeb webObject, Graphics g, MasterPane pane)
    {
        //获取参数信息
       
        List<double> AxisYvalueList = new List<double>();
        for (int i = 0; i < AxisValueList.Count(); i++)
        {
            if(AxisValueList[i].YAxis != null)
            {
                AxisYvalueList.Add(AxisValueList[i].YAxis);
            }
            
        }
        double AxisYvalueListMax = Convert.ToDouble(AxisYvalueList.Max());
        int YAxisMax = ((int)(AxisYvalueListMax / 10)) * 10 + 20;//最大值可以计算，从数据点中提取y最大值整除10，得到的结果乘以10再加20就是最大值刻度，为下面textObj定位做好准备
        //拥有一个MasterPane实例包含一个默认的GraphPane
        GraphPane myPane = pane[0];
        myPane.Legend.IsVisible = true;//不显示图例
        myPane.Title.Text = "Test Distribution Report" ; //图片的标题      
        myPane.XAxis.Title.Text = _CPKParameterValueInfo.TitleXAxis;//X轴的标题   
        myPane.XAxis.Scale.MinorStep = _CPKParameterValueInfo.XAxisMinorStep;//设置次刻度的步进值
        myPane.XAxis.Scale.MajorStep = _CPKParameterValueInfo.XAxisMajorStep;//设置主刻度的步进值
        //  myPane.XAxis.MajorGrid.IsVisible = true;
        //  myPane.XAxis.MajorGrid.DashOff = 0;//表示为实线
        myPane.YAxis.Title.Text = "Count"; //Y轴的标题
        myPane.YAxis.MajorGrid.IsVisible = true;
        myPane.YAxis.Scale.Max = YAxisMax;//设置纵坐标最大值
        myPane.YAxis.Scale.MajorStep = 10;//设置刻度的步进值
        myPane.YAxis.MajorGrid.DashOff = 0;//表示为实线
        myPane.YAxis.MajorGrid.PenWidth = 1f;
        PointPairList list = new PointPairList(); //保存数据的集合           
        for (int i = 0; i < AxisValueList.Count(); i++)
        {
            list.Add(Convert.ToDouble( AxisValueList[i].XAxis), Convert.ToDouble( AxisValueList[i].YAxis));
        }
        BarItem myCurve = myPane.AddBar(testType, list, Color.Blue);//产生一蓝条
        
       myCurve.Bar.Fill = new Fill(Color.Blue, Color.White, Color.Blue,10f);//填充这个蓝条,让这蓝条看起来有3D的效果
        //  BarItem myCurve2 = myPane.AddBar("新会员", list2, Color.Red);
        //myCurve2.Bar.Fill = new Fill(Color.Red, Color.White, Color.Red);
       // myCurve.Bar.Border.Width = 3f;//柱状图边框厚度
        //   myPane.BarSettings.Type = ZedGraph.BarType.Cluster;//设置柱子宽度时可能需要该设置
        //  myPane.BarSettings.ClusterScaleWidth = 0.02f;//柱子的宽度
        myPane.BarSettings.Type = BarType.Cluster; //设置Bar的显示,默认为Cluster,可以不用设置
        myPane.XAxis.MajorTic.IsBetweenLabels = true; //设置为True时,每个Bar(柱体)之间会有一个小分隔线,为False时,这个小分隔线会出现在Bar(柱体)的正中间
        //   string[] labels = { "亚洲", "美洲", "非洲", "欧洲", "大洋洲" };
        //  myPane.XAxis.Scale.TextLabels = labels; //X轴的说明文字
        myPane.XAxis.Type = AxisType.Linear;      //X轴类型
       // myPane.Fill = new Fill(Color.White, Color.FromArgb(200, 200, 255), 45.0f); //以渐变色填充窗体的背景
        myPane.Fill = new Fill(Color.White, Color.FromArgb(255, 255, 255), 45.0f); //以渐变色填充窗体的背景
        //myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f); //以渐变色填充图表的背景
        myPane.Chart.Fill = new Fill(Color.White, Color.White, 45.0f); //以渐变色填充图表的背景
        //更新图表
        if (_CPKParameterValueInfo.USL != "NULL")
        {
            LineObj threshusl = new LineObj(Color.Red, Convert.ToDouble(_CPKParameterValueInfo.USL), 0, Convert.ToDouble(_CPKParameterValueInfo.USL), YAxisMax);//画线
            myPane.GraphObjList.Add(threshusl);
            TextObj textLabelUsl = new TextObj();
            myPane.GraphObjList.Add(ReturnTextObj(textLabelUsl, "USL", Convert.ToDouble(_CPKParameterValueInfo.USL), YAxisMax));//添加标签
        }
        if (_CPKParameterValueInfo.LSL != "NULL")
        {
            LineObj threshlsl = new LineObj(Color.Red, Convert.ToDouble(_CPKParameterValueInfo.LSL), 0, Convert.ToDouble(_CPKParameterValueInfo.LSL), YAxisMax);//画线
              myPane.GraphObjList.Add(threshlsl);
              TextObj textLabelLsl = new TextObj();
              myPane.GraphObjList.Add(ReturnTextObj(textLabelLsl, "LSL", Convert.ToDouble(_CPKParameterValueInfo.LSL), YAxisMax));//添加标签
        }  
        LineObj threshmean = new LineObj(Color.Blue, Convert.ToDouble(_CPKParameterValueInfo.Mean), 0,Convert.ToDouble( _CPKParameterValueInfo.Mean), YAxisMax);
        LineObj threshPositiveThreeSD = new LineObj(Color.Blue, Convert.ToDouble(_CPKParameterValueInfo.PositiveThreeSD), 0, Convert.ToDouble(_CPKParameterValueInfo.PositiveThreeSD), YAxisMax);
        LineObj threshNegativeThreeSD = new LineObj(Color.Blue, Convert.ToDouble(_CPKParameterValueInfo.NegativeThreeSD), 0, Convert.ToDouble(_CPKParameterValueInfo.NegativeThreeSD), YAxisMax);

        // threshlsl.Line.Color = Color.Blue;
        threshmean.Line.Style = System.Drawing.Drawing2D.DashStyle.Dash;//虚线
        threshPositiveThreeSD.Line.Style = System.Drawing.Drawing2D.DashStyle.Dash;
        threshNegativeThreeSD.Line.Style = System.Drawing.Drawing2D.DashStyle.Dash;
        //threshlsl.Line.DashOn = 20f;
        // threshlsl.Line.Width = 1.9f;//线的宽度

        myPane.GraphObjList.Add(threshmean);
        myPane.GraphObjList.Add(threshPositiveThreeSD);
        myPane.GraphObjList.Add(threshNegativeThreeSD);
           
        TextObj textLabelMean = new TextObj();
        TextObj textLabelPositiveThreeSD = new TextObj();
        TextObj textLabelNegativeThreeSD = new TextObj();
   
        myPane.GraphObjList.Add(ReturnTextObj(textLabelMean, "Mean", Convert.ToDouble(_CPKParameterValueInfo.Mean), YAxisMax));
        myPane.GraphObjList.Add(ReturnTextObj(textLabelPositiveThreeSD, "+3σ", Convert.ToDouble(_CPKParameterValueInfo.PositiveThreeSD), YAxisMax));
        myPane.GraphObjList.Add(ReturnTextObj(textLabelNegativeThreeSD, "-3σ", Convert.ToDouble(_CPKParameterValueInfo.NegativeThreeSD), YAxisMax));
        //   LineItem curve = myPane.AddCurve("label", list, Color.Red, SymbolType.Circle);
        //  curve.Line.IsVisible = false;
        // myPane.GraphObjList.Add(threshlsl3);
        pane.AxisChange(g);
    }

    private string GetTypeValue(string _DropDownListSelected)
    {
        if(_DropDownListSelected=="第一次测试CPK")
        {
            return "FirstTest";
        }
        else
        {
            return "FinalTest";
        }
    }
  
}